package com.xbongbong.pro.statistic.enums;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.constant.PaasConstant;
import lombok.Getter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

/**
 * 图表编辑操作按钮枚举
 *
 * @author zyy
 * @date 2022/11/17/ 14:16:00
 * @since v1.0
 */
@Getter
public enum ChartEditOpEnum implements Serializable {

    /**
     * 复制
     */
    COPY("copy","enum.chartEditOpEnum.copy","web-icon-field-subform web-iconfont",0),

    /**
     * 编辑
     */
    EDIT("edit","enum.chartEditOpEnum.edit","web-icon-bianji web-iconfont",10),

    /**
     * 删除
     */
    DEL("del","enum.chartEditOpEnum.del","web-icon-shanchu_xian web-iconfont",20),


    ;

    /**
     * 别名，与前端对应
     */
    private String alias;

    /**
     * 鼠标悬浮时的提示
     */
    private String name;

    /**
     * 按钮图标
     */
    private String icon;

    /**
     * 展示排序
     */
    private int sort;




    ChartEditOpEnum(String alias, String name,String icon,int sort) {
        this.alias = alias;
        this.name = name;
        this.icon = icon;
        this.sort = sort;
    }


    public String getName() {
        return I18nMessageUtil.getMessage(name);
    }

    /**
     * 获取所有枚举，并排序(默认升序)
     *
     * @return java.util.List<com.xbongbong.pro.statistic.enums.ChartEditOpEnum>
     * @author zyy
     * @date 2022/11/17
     * @version v1.0
     */
    public static List<ChartEditOpEnum> getBaseOpList(Comparator<? super ChartEditOpEnum> comparator){
        List<ChartEditOpEnum> baseOpList = new ArrayList<>(Arrays.asList(values()));
        if(Objects.isNull(comparator)){
            comparator = Comparator.comparingInt(ChartEditOpEnum::getSort);
        }
        baseOpList.sort(comparator);
        return baseOpList;
    }

    /**
     * 枚举转json
     *
     * @param chartEditOpEnumList
     * @return com.alibaba.fastjson.JSONArray
     * @author zyy
     * @date 2022/11/17
     * @version v1.0
     */
    public static JSONArray getEnumList(List<ChartEditOpEnum> chartEditOpEnumList){
        JSONArray jsonArray = new JSONArray();
        for (ChartEditOpEnum chartEditOpEnum : chartEditOpEnumList) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put(PaasConstant.ALIAS, chartEditOpEnum.getAlias());
            jsonObject.put(PaasConstant.NAME, chartEditOpEnum.getName());
            jsonObject.put(PaasConstant.ICON, chartEditOpEnum.getIcon());
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }

}
